<?php

class Admin extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('parser');
        $this->load->model('uwd');
        $login = $this->session->userdata('login');
        $privilege = $this->session->userdata('pri');
        if ($login != TRUE || $privilege != 1) {
            redirect('main/logout');
        }
    }

    function index() {
        redirect('admin/home');
    }

    function home() {
        $id = $this->session->userdata('username');
        $data['nama'] = $id;
        $data['page'] = 'Home Dosen | Universitas Widya Dharma';
        $this->parser->parse('themes/header', $data);
        $this->load->view('admin/nav');
        $this->load->view('themes/content');
        $this->load->view('themes/footer');
    }

    function password() {
        $data['page'] = 'Ganti Password | Universitas Widya Dharma';
        $id = $this->session->userdata('username');
        $data['nama'] = $id;
        $this->parser->parse('themes/header', $data);
        $this->load->view('admin/nav');
        $this->parser->parse('admin/password', $data);
        $this->load->view('themes/footer');
    }

    function password_update() {
        $lama = $this->input->post('lama');
        $id = $this->session->userdata('username');
        $cek = $this->uwd->passlama($id, $lama);
        if ($cek == TRUE) {
            $pass = array(
                'password' => $this->input->post('baru')
            );
            $this->uwd->passbaru($id, $pass);
            $data['nama'] = $id;
            $data['page'] = 'Berhasil Update Password | Universitas Widya Dharma';
            $data['alert'] = '<div class="alert alert-success"><button type="button" class="close" data-dismiss="alert">×</button><strong>Password berhasil dirubah.</strong></div>';
            $this->parser->parse('themes/header', $data);
            $this->load->view('admin/nav');
            $this->parser->parse('themes/alert', $data);
            $this->load->view('themes/footer');
        } else {
            $data['page'] = 'Gagal Update Password | Universitas Widya Dharma';
            $data['nama'] = $id;
            $data['alert'] = '<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">×</button><strong>Password lama tidak cocok.</strong></div>';
            $this->parser->parse('themes/header', $data);
            $this->load->view('admin/nav');
            $this->parser->parse('themes/alert', $data);
            $this->load->view('themes/footer');
        }
    }

    function dosen() {
        $this->load->library('pagination');
        $id = $this->session->userdata('username');
        $data = array(
            'page' => 'Dosen | Universitas Widya Dharma',
            'nama' => $id
        );
        $config['base_url'] = $this->config->config['base_url'] . '/index.php/admin/dosen/';
        $config['total_rows'] = $this->uwd->count_dosen();
        $config['per_page'] = 10;
        $config['num_links'] = 2;
        $config['uri'] = $this->uri->segment(3);
        $this->pagination->initialize($config);
        if ($this->uwd->list_dosen($config)) {
            $data['dosen'] = $this->uwd->list_dosen($config);
        } else {
            $data['dosen'] = array();
        }
        $this->parser->parse('themes/header', $data);
        $this->load->view('admin/nav');
        $this->parser->parse('admin/dosen', $data);
        $this->load->view('themes/footer');
    }

    function add_dosen() {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('nama', 'Nama dosen', 'required');
        if ($this->form_validation->run() == FALSE) {
            $id = $this->session->userdata('username');
            $data = array(
                'page' => 'Dosen | Universitas Widya Dharma',
                'nama' => $id
            );
            $this->parser->parse('themes/header', $data);
            $this->load->view('admin/nav');
            $this->parser->parse('admin/dosen', $data);
            $this->load->view('themes/footer');
        } else {
            $username = $this->input->post('username');
            $nama = $this->input->post('nama');
            $usr = array(
                'username' => $username,
                'password' => $username,
                'privilegeId' => 2
            );
            $this->uwd->add_user($usr);
            $data = array(
                'username' => $username,
                'nama' => $nama
            );
            $this->uwd->add_dosen($data);
            redirect('admin/dosen');
        }
    }

    function matkul() {
        $this->load->library('pagination');
        $id = $this->session->userdata('username');
        $data = array(
            'page' => 'Mata Kuliah | Universitas Widya Dharma',
            'nama' => $id
        );
        $config['base_url'] = $this->config->config['base_url'] . '/index.php/admin/matkul/';
        $config['total_rows'] = $this->uwd->count_matkul();
        $config['per_page'] = 10;
        $config['num_links'] = 2;
        $config['uri'] = $this->uri->segment(3);
        $this->pagination->initialize($config);
        if ($this->uwd->list_matkul($config)) {
            $data['matkul'] = $this->uwd->list_matkul($config);
        } else {
            $data['matkul'] = array();
        }
        $this->parser->parse('themes/header', $data);
        $this->load->view('admin/nav');
        $this->parser->parse('admin/matkul', $data);
        $this->load->view('themes/footer');
    }

    function add_matkul() {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('matkul', 'Mata Kuliah', 'required');
        if ($this->form_validation->run() == FALSE) {
            $id = $this->session->userdata('username');
            $data = array(
                'page' => 'Mata Kuliah | Universitas Widya Dharma',
                'nama' => $id
            );
            $this->parser->parse('themes/header', $data);
            $this->load->view('admin/nav');
            $this->parser->parse('admin/matkul', $data);
            $this->load->view('themes/footer');
        } else {
            $matkul = $this->input->post('matkul');
            $mtk = array(
                'matkul' => $matkul,
            );
            $this->uwd->add_matkul($mtk);
            redirect('admin/matkul');
        }
    }

    function del_dosen() {
        $id = $this->uri->segment(3);
        $this->uwd->del_dosen($id);
        redirect('admin/dosen');
    }

    function del_matkul() {
        $id = $this->uri->segment(3);
        $this->uwd->del_matkul($id);
        redirect('admin/matkul');
    }

    function edit_matkul() {
        $id = $this->uri->segment(3);
        $da = $this->uwd->get_matkul($id);
        $data = array(
            'id' => $da->id,
            'matkul' => $da->matkul,
            'page' => 'Edit Mata Kuliah | Universitas Widya Dharma',
            'nama' => $id
        );
        $this->parser->parse('themes/header', $data);
        $this->load->view('admin/nav');
        $this->parser->parse('admin/edit_matkul', $data);
        $this->load->view('themes/footer');
    }

    function upd_matkul() {
        $id = $this->input->post('id');
        $data = array(
            'matkul' => $this->input->post('matkul')
        );
        $this->uwd->upd_matkul($id, $data);
        redirect('admin/matkul');
    }

}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
?>
